/* 
* @Author: Turbidsoul Chen
* @Date:   2016-11-28 15:32:57
* @Last Modified by:   Turbidsoul
* @Last Modified time: 2017-01-15 14:31:35
*/

import {observable, computed, autorun} from 'mobx';

export default class TodoStore {
  @observable todos = [];
  @observable pendingRequests = 0;

  constructor() {
    let _todos = localStorage.getItem('todos');
    if(_todos !== null) {
      this.todos = JSON.parse(_todos);
    }
    autorun(()=>console.log(this.report));
  }

  @computed get completedTodosCount() {
    return this.todos.filter(todo=>todo.completed === true).length;
  }

  @computed get report() {
    if (this.todos.length === 0) {
      return '<none>';
    }
    return `Next todo: "${this.todos[0].task}". Progress: ${this.completedTodosCount}/${this.todos.length}`;
  }

  @computed get todoList() {
    return this.todos;
  }

  addTodo(task) {
    this.todos.push({
      task,
      completed: false,
      assignee: null
    });
    localStorage.setItem('todos', JSON.stringify(this.todos));
  }

  removeTodo(i) {
    console.log(i);
    this.todos.splice(i, i);
    localStorage.setItem('todos', JSON.stringify(this.todos));
  }

  toggleCompleted(i) {
    this.todos[i].completed = !this.todos[i].completed;
    localStorage.setItem('todos', JSON.stringify(this.todos));
  }
}
